﻿create PROCEDURE Sync.SP_CommittessVillages_ApplyChanges
@RawValues XML 
AS 
BEGIN
	 
		DECLARE @CommittessVillages_Temp TABLE 
		(
			CommitteID UniqueIdentifier,
	VillageID UniqueIdentifier,
			
			IsDeleted BIT, 
			LastUpdatedDate DATETIME
		)
		
	   INSERT INTO @CommittessVillages_Temp 
	   SELECT	Tbl.Col.value('CommitteID[1]','uniqueidentifier') CommitteID,
Tbl.Col.value('VillageID[1]','uniqueidentifier') VillageID,
                
				Tbl.Col.value('IsDeleted[1]', 'bit') IsDeleted,
				Tbl.Col.value('LastUpdatedDate[1]','DateTime') LastUpdatedDate
	   FROM   @RawValues.nodes('/DocumentElement/CommittessVillages') Tbl(Col)
----------------------------------------------------------------------------------------

-- Insert ------------------------------------------------------------------------------

	INSERT INTO [dbo].[CommittessVillages] ([CommitteID],[VillageID])
	SELECT t.[CommitteID],t.[VillageID]
	FROM  @CommittessVillages_Temp t
	WHERE t.IsDeleted = 0
    AND NOT EXISTS
    (
        SELECT 1 FROM sync.CommittessVillages_Tracking s
        WHERE t.CommitteID = s.CommitteID AND t.VillageID = s.VillageID
    )
	-- put conflicts here.
	
-- Update -------------------------------------------------------------------------------
    -----------------------------------------------------------------------------------------

-- Delete -------------------------------------------------------------------------------

	DELETE [dbo].[CommittessVillages]
    FROM @CommittessVillages_Temp t JOIN [dbo].[CommittessVillages] s
    ON t.CommitteID = s.CommitteID AND t.VillageID = s.VillageID
	WHERE t.IsDeleted = 1

-----------------------------------------------------------------------------------------
	
END











